$(document).ready(function(){
var t=-1;
var c=-1;
var r=-1;
var u=-1;
var co=-1;
var verrou=0;
var cpt_edit_txt=-1;
var cpt_edit_chk=0;
var cpt_edit_radio=0;
var cpt_edit_upload=0;
var cpt_edit_cond=0;
  // ajouter le text 
              // ajouter le text 
  
    $("#Create_input_text").click(function(){
        t++;
        var sourcetxt=$("#text").html();
        sourcetxt=sourcetxt.replace("id_div_txt","id_div_txt"+t);
        sourcetxt=sourcetxt.replace("text_id","text_id"+t);
        sourcetxt=sourcetxt.replace("text_name","text_name"+t);
        sourcetxt=sourcetxt.replace("boutton_edit_text","boutton_edit_text"+t);
        sourcetxt=sourcetxt.replace("label_txt","label_txt"+t);
        sourcetxt=sourcetxt.replace("grey-bg","grey-bg champ");
        $(".source_interface").append(sourcetxt);
        $("#label_txt"+t).attr("class","voir");
        $("#text_id"+t).attr("class","voir");
    });

    //ajouter checkbox
    $("#Create_input_checkbox").click(function(){
        c++;
        var sourcetxt=$("#checkbox").html();
        sourcetxt=sourcetxt.replace("id_div_checkbox","id_div_checkbox"+c);
        sourcetxt=sourcetxt.replace("checkbox_id","checkbox_id"+c);
        sourcetxt=sourcetxt.replace("checkbox_name","checkbox_name"+c);
        sourcetxt=sourcetxt.replace("label_checkbox","label_checkbox"+c);
        sourcetxt=sourcetxt.replace("grey-bg","grey-bg champ");
        $(".source_interface").append(sourcetxt);
        $("#label_checkbox"+c).attr("class","voir");
        $("#checkbox_id"+c).attr("class","voir");
    });

    //ajouter radio 
    $("#Create_input_radio").click(function(){
        r++;
        var sourcetxt=$("#radio").html();
        sourcetxt=sourcetxt.replace("id_div_radio","id_div_radio"+r);
        sourcetxt=sourcetxt.replace("radio_id","radio_id"+r);
        sourcetxt=sourcetxt.replace("radio_name","radio_name"+r);
        sourcetxt=sourcetxt.replace("label_radio","label_radio"+r);
        sourcetxt=sourcetxt.replace("grey-bg","grey-bg champ");
        $(".source_interface").append(sourcetxt);
        $("#label_radio"+r).attr("class","voir");
        $("#radio_id"+r).attr("class","voir");
    });

    //ajouter un upload
    $("#Create_input_file_upload").click(function(){
        u++;
        var sourcetxt=$("#upload").html();
        sourcetxt=sourcetxt.replace("id_div_upload","id_div_upload"+u);
        sourcetxt=sourcetxt.replace("upload_id","upload_id"+u);
        sourcetxt=sourcetxt.replace("upload_name","upload_name"+u);
        sourcetxt=sourcetxt.replace("label_upload","label_upload"+u);
        sourcetxt=sourcetxt.replace("grey-bg","grey-bg champ");
        $(".source_interface").append(sourcetxt);
        $("#label_upload"+u).attr("class","upload voir");
        $("#upload_id"+u).attr("class","upload voir");
    });
         
    // ajouter une condition 
    $("#Create_input_Condition").click(function(){
        co++;                    
        var sourcetxt=$("#conditionnel").html();
        sourcetxt=sourcetxt.replace("id_div_cond","id_div_cond"+co);
        sourcetxt=sourcetxt.replace("cond_id","cond_id"+co);
        sourcetxt=sourcetxt.replace("cond_name","cond_name"+co);
        sourcetxt=sourcetxt.replace("label_cond","label_cond"+co);
        sourcetxt=sourcetxt.replace("edit_show_cond","edit_show_cond"+co);
        sourcetxt=sourcetxt.replace("grey-bg","grey-bg champ");
        $(".source_interface").append(sourcetxt);
        $("#label_cond"+co).attr("class","voir");
        $("#cond_id"+co).attr("class","voir");
    });
          
         
    //Supprimer
    $("#supprimer").live("click",function(){
        $(this).parents("fieldset:first").remove();
    });

    //reduire
    $("#reduire").live("click",function () {
        verrou=0;
        $(this).parent("div").children('div:first').html("");
    });

    //editionner le text 
    $(".button_edit_text").live("click",function(){
        if (verrou==0)
        {
            cpt_edit_txt++;
            var sourcetxt=$("#interface_edit_txt").html();
            sourcetxt=sourcetxt.replace("source_taille","source_taille"+cpt_edit_txt);
            sourcetxt=sourcetxt.replace("source_label_text","source_label_text"+cpt_edit_txt);
            sourcetxt=sourcetxt.replace("source_nom_txt","source_nom_txt"+cpt_edit_txt);

            $(this).parent("div:first").children("div").append(sourcetxt);
            verrou=1;
        }
    });

    //editionner checkbox 
    $(".bouton_edit_checkbox").live("click",function(){
        if (verrou==0)
        {
            cpt_edit_chk++;
            var sourcetxt=$("#interface_edit_checkbox").html();

            sourcetxt=sourcetxt.replace("source_label_checkbox","source_label_checkbox"+cpt_edit_chk);
            sourcetxt=sourcetxt.replace("source_nom_checkbox","source_nom_checkbox"+cpt_edit_chk);
            $(this).parent("div").children("div").append(sourcetxt);
            verrou=1;
        }
    });
				
    // editionner radio
    $(".boutton_edit_radio").live("click",function(){
        if (verrou==0)
        {
            cpt_edit_radio++;
            var sourcetxt=$("#interface_edit_radio").html();

            sourcetxt=sourcetxt.replace("source_label_radio","source_label_radio"+cpt_edit_chk);
            sourcetxt=sourcetxt.replace("source_nom_radio","source_nom_radio"+cpt_edit_chk);
            $(this).parent("div").children("div").append(sourcetxt);
            verrou=1;
        }
    });
    //edit radio
    $(".boutton_edit_radio").live("click",function(){
        if (verrou==0)
        {
            cpt_edit_radio++;
            var sourcetxt=$("#interface_edit_radio").html();

            sourcetxt=sourcetxt.replace("source_label_radio","source_label_radio"+cpt_edit_chk);
            sourcetxt=sourcetxt.replace("source_nom_radio","source_nom_radio"+cpt_edit_chk);
            $(this).parent("div").children("div").append(sourcetxt);
            verrou=1;
        }
    });
                
    //editionner condition
    $(".bouton_edit_cond").live("click",function(){
        if (verrou==0)
        {
            cpt_edit_upload++;
            var sourcetxt=$("#interface_edit_cond").html();

            sourcetxt=sourcetxt.replace("source_label_cond","source_label_cond"+cpt_edit_cond);
            sourcetxt=sourcetxt.replace("source_nom_cond","source_nom_cond"+cpt_edit_cond);
            $(this).parent("div").children("div:first").append(sourcetxt);
            verrou=1;   
        }
               
    });
                
    //desaciver lajout normal et activer a la condition
    $(".Ajout_cond").live("click",function(){
        $("#source").attr("class","");
        $(this).parent('div').children('div:eq(1)').attr("class","source_interface");
        $('#id_div_txt').children('label').attr("cond","cond_champ"+co);
        $('#id_div_txt').children(':input').attr("cond","cond_champ"+co); 
        $('#id_div_checkbox').children('label').attr("cond","cond_champ"+co);
        $('#id_div_checkbox').children(':input').attr("cond","cond_champ"+co);
        $('#id_div_radio').children('label').attr("cond","cond_champ"+co);
        $('#id_div_radio').children(':input').attr("cond","cond_champ"+co);
    });
                
    //retour a letat normal
    $(".desactiver_cond").live("click",function(){
        $("#source").attr("class","source_interface");
        $(this).parent('div').children('div:eq(1)').attr("class","");
        $('#id_div_txt').children('label').attr("cond","");                                      
        $('#id_div_txt').children(':input').attr("cond","");  
        $('#id_div_checkbox').children('label').attr("cond","");
        $('#id_div_checkbox').children(':input').attr("cond","");
        $('#id_div_radio').children('label').attr("cond","");
        $('#id_div_radio').children(':input').attr("cond","");
    
    });
                
    //Validation des modification condition
    $(".valider_modif_cond").live("click",function(){               
        var source_label=$(this).parent("div").children(":input:first").val();
        var source_name=$(this).parent("div").children("input:eq(1)").val();
        $(this).parent("div").parent("div").children("input#").attr("name",source_name);
        $(this).parent("div").parent("div").children("label").text(source_label);
                   
    });

    //Validation des modification text
    $(".valider_modif_text").live("click",function(){
        //contenu du champ text
        var source_size=$(this).parent("div").children(":input:first").val();
        var source_Label=$(this).parent("div").children("input:eq(1)").val();
        var source_name=$(this).parent("div").children("input:eq(2)").val();

        $(this).parent("div").parent("div").children("input#").attr("size",source_size);
        $(this).parent("div").parent("div").children("input#").attr("name",source_name);
        $(this).parent("div").parent("div").children("label").text(source_Label);

        if ($(this).parent("div").children("input:eq(3)").is(':checked'))
        {
            $(this).parent("div").parent("div").children("input#").attr("req","oui");
            $(this).parent("div").parent("div").children("label").attr("class","voir required");
        }
        else
        {
            $(this).parent("div").parent("div").children("input#").attr("req","non");
            $(this).parent("div").parent("div").children("label").attr("class","voir");
        }
    });

    //Validation des modification checkbox
    $(".valider_modif_checkbox").live("click",function(){
        //contenu du champ checkbox

        var source_label=$(this).parent("div").children(":input:first").val();
        var source_name=$(this).parent("div").children("input:eq(1)").val();
        $(this).parent("div").parent("div").children("input#").attr("name",source_name);
        $(this).parent("div").parent("div").children("label").text(source_label);

        if ($(this).parent("div").children("input:eq(2)").is(':checked'))
        {
            $(this).parent("div").parent("div").children("input#").attr("req","oui");
            $(this).parent("div").parent("div").children("label").attr("class","voir required");
        }
        else
        {
            $(this).parent("div").parent("div").children("input#").attr("req","non");
            $(this).parent("div").parent("div").children("label").attr("class","voir");
        }
    });



    //validate modif radio
    $(".valider_modif_radio").live("click",function(){
        //contenu des modifications radio

        var source_label=$(this).parent("div").children(":input:first").val();
        var source_name=$(this).parent("div").children("input:eq(1)").val();
        $(this).parent("div").parent("div").children("input#").attr("name",source_name);
        $(this).parent("div").parent("div").children("label").text(source_label);

        if ($(this).parent("div").children("input:eq(2)").is(':checked'))
        {
            $(this).parent("div").parent("div").children("input#").attr("req","oui");
            $(this).parent("div").parent("div").children("label").attr("class","voir required");
        }
        else
        {
            $(this).parent("div").parent("div").children("input#").attr("req","non");
            $(this).parent("div").parent("div").children("label").attr("class","voir");
        }
    });
                
    //validate modif upload
    $(".valider_modif_upload").live("click",function(){
        //contenu des modifications upload

        var source_label=$(this).parent("div").children(":input:first").val();
        var source_name=$(this).parent("div").children("input:eq(1)").val();
        var source_type=$(this).parent("div").children("input:eq(2)").val();
                    
        $(this).parent("div").parent("div").children("input#").attr("name",source_name);
        $(this).parent("div").parent("div").children("input#").attr("type_file",source_type);
        $(this).parent("div").parent("div").children("label").text(source_label);
                    
        if ($(this).parent("div").children("input:eq(3)").is(':checked'))
        {
            $(this).parent("div").parent("div").children("input#").attr("req","oui");
            $(this).parent("div").parent("div").children("label").attr("class","upload voir required");
        }
        else
        {
            $(this).parent("div").parent("div").children("input#").attr("req","non");
            $(this).parent("div").parent("div").children("label").attr("class","upload voir");
        }
    });
			
				
    //Envoyer les données du formulaire 
    $("#send").live("submit",function(event){

        event.preventDefault(); 
        //compteur des champs 
        cpt_text=0;
        cpt_radio=0;
        cpt_checkbox=0;
        cpt_upload=0;
        cpt_matrice=0;
        cpt_cond=0;
                    
        <!-- variable du champ text -->
        var val_label;
        var attr_label;
        var id_label="label_txt";
        var id_text="text_id";
        var name_txt="text_name";
        var size_txt;
        var req_txt;
        var matrice_txt=new Array();


        // variable du champ checkbox
        var id_label_checkbox="label_checkbox";
        var id_checkbox="checkbox_id";
		
        // variable du champ radio
        var id_label_radio="label_radio";
        var id_radio="radio_id";
                
        // variable du champ upload   
        var type_permi;
        var id_label_upload="label_upload";
        var id_upload="upload_id";
                   
        // variable du champ condition 
        var id_label_cond="label_cond";
        var id_cond="cond_id";
                    
        // parcourire les champs 
        $(".champ").each(function(){	
            var tab_txt=new Array();
            cpt_matrice++;
            //insertion d'un type text
            if ($(this).children("div").attr("class")=="text_champ")
            {
                cpt_text++;
	
                id_label+=cpt_text;
                id_text+=cpt_text;

                val_label=$("#"+id_label).text();
                class_label=$("#"+id_label).attr("class");

                name_text=$("#"+id_text).attr("name");
                size_txt=$("#"+id_text).attr("size");
                req_text=$("#"+id_text).attr("req");
                condition=$("#"+id_text).attr("cond");
                           
                tab_txt[0]="text";
                tab_txt[1]=id_label;
                tab_txt[2]=val_label;
                tab_txt[3]=class_label;
                tab_txt[4]=name_text;
                tab_txt[5]=size_txt;
                tab_txt[6]=req_text;
                tab_txt[7]=cpt_text;
                tab_txt[8]=condition;
                                
                matrice_txt[cpt_matrice-1]=tab_txt;
                id_label="label_txt";
                id_text="text_id";
            }
            //insertion des upload
            else if ($(this).children("div").attr("class")=="upload_champ")
            {
                cpt_upload++;
	
                id_label_upload+=cpt_upload;
                id_upload+=cpt_upload;

                val_label=$("#"+id_label_upload).text();
                class_label=$("#"+id_label_upload).attr("class");
                name_upload=$("#"+id_upload).attr("name");
                type_permi=$("#"+id_upload).attr("type_file");
                req_upload=$("#"+id_upload).attr("req");

                tab_txt[0]="upload";
                tab_txt[1]=id_label_upload;
                tab_txt[2]=val_label;
                tab_txt[3]=class_label;
                tab_txt[4]=name_upload;
                tab_txt[5]=type_permi;
                tab_txt[6]=req_upload;
                tab_txt[7]=cpt_upload;
                            
                matrice_txt[cpt_matrice-1]=tab_txt;
                id_label="label_upload";
                id_text="text_upload";
            }
                       
            //insertion des checkbox 
            else if ($(this).children("div").attr("class")=="checkbox_champ")
            {
                cpt_checkbox++

                id_label_checkbox+=cpt_checkbox;
                id_checkbox+=cpt_checkbox;
                val_label=$("#"+id_label_checkbox).text();
                class_label=$("#"+id_label_checkbox).attr("class");
                name_checkbox=$("#"+id_checkbox).attr("name");
                req_checkbox=$("#"+id_checkbox).attr("req");
                condition=$("#"+id_checkbox).attr("cond");
                           
                tab_txt[0]="checkbox";
                tab_txt[1]=id_label_checkbox;
                tab_txt[2]=val_label;
                tab_txt[3]=class_label;
                tab_txt[4]=name_checkbox;
                tab_txt[5]=req_checkbox;
                tab_txt[6]=cpt_checkbox;
                tab_txt[7]=condition;
                            
                matrice_txt[cpt_matrice-1]=tab_txt;
                id_label_checkbox="label_checkbox";
                id_checkbox="checkbox_id";
            }
            //insersion des radio
            else if ($(this).children("div").attr("class")=="radio_champ")
            {
						
                cpt_radio++

                id_label_radio+=cpt_radio;
                id_radio+=cpt_radio;
                val_label=$("#"+id_label_radio).text();
                class_label=$("#"+id_label_radio).attr("class");
                name_radio=$("#"+id_radio).attr("name");
                req_radio=$("#"+id_radio).attr("req");
                condition=$("#"+id_radio).attr("cond");
                            
                tab_txt[0]="radio";
                tab_txt[1]=id_label_radio;
                tab_txt[2]=val_label;
                tab_txt[3]=class_label;
                tab_txt[4]=name_radio;
                tab_txt[5]=req_radio;
                tab_txt[6]=cpt_radio;
                tab_txt[7]=condition;
                           
                matrice_txt[cpt_matrice-1]=tab_txt;
                id_label_radio="label_radio";
                id_radio="radio_id";
            }
			
            // champ condition 
                
            else if ($(this).children("div").attr("class")=="champ_cond")
            {
                cpt_cond++;
                id_label_cond+=cpt_cond;
                id_cond+=cpt_cond;
                val_label=$("#"+id_label_cond).text();
                class_label=$("#"+id_label_cond).attr("class");
                name_cond=$("#"+id_cond).attr("name");
                class_cond=$("#"+id_cond).attr("class");
                req_cond=$("#"+id_cond).attr("req");

                tab_txt[0]="cond";
                tab_txt[1]=id_label_cond;
                tab_txt[2]=val_label;
                tab_txt[3]=class_label;
                tab_txt[4]=name_cond;
                tab_txt[5]=class_cond;
                tab_txt[6]=req_cond;
                tab_txt[7]=cpt_cond;       
                     
                         
                matrice_txt[cpt_matrice-1]=tab_txt;
                id_label_cond="label_cond";
                id_cond="cond_id";
            }
        });
                    
        var nom=$("#name_form").attr("value");
        var edit=$("#edit").html();
        $.post("recuperation",{
            contenu :matrice_txt,
            form_name :nom,
            nb_champ :cpt_matrice,
            edit_code:edit
        });

        var source_save=$("#enregistrer").html();
        source_save=source_save.replace("hidden","visible");
        $("#affiche_enregistrer").html("");
        $("#affiche_enregistrer").append(source_save);
    });

    <!-- apperçu -->
    $("#boutton_appercu").live("click",function(){    
        $(".voir").each(function(){
            var res=$(this).clone();
            $(".affich").attr("style","visibility:visible;");
            $("#div-truc").append("<br>");
            $("#div-truc").append(res);
            $("#div-truc").append("<hr>");

        })
           
    });
    <!-- fermer apperç -->
    $("#close").live("click",function(){
        $(".affich").attr("style","visibility:hidden;");
        $("#div-truc").html("");
    });

});